/*
 * Copyright (C), 2014-2015, 杭州小卡科技有限公司
 * Created by ciwei@xiaokakeji.com on 2016/06/04.
 */
package com.micromall.service;

import com.micromall.repository.FreightRulesMapper;
import com.micromall.repository.FreightTemplateMapper;
import com.micromall.repository.entity.FreightRules;
import com.micromall.repository.entity.FreightTemplate;
import com.micromall.repository.entity.OrderGoods;
import com.micromall.repository.entity.ShippingAddress;
import com.micromall.utils.Condition.Criteria;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * @author ciwei@xiaokakeji.com
 * @date 2016/06/04.
 */
@Service
@Transactional
public class FreightTemplateService {

	@Resource
	private FreightTemplateMapper freightTemplateMapper;
	@Resource
	private FreightRulesMapper    freightRulesMapper;

	@Transactional(readOnly = true)
	public BigDecimal calculate(ShippingAddress address, List<OrderGoods> goodsList) {
		// 找到合适的运费模板
		for (OrderGoods goods : goodsList) {
			FreightTemplate template = freightTemplateMapper.selectByPrimaryKey(goods.getFreightId());
			goods.setFreightTemplate(template);
			if (template.getInclPostage()) {
				continue;
			}
			List<FreightRules> _rulesList = freightRulesMapper
					.selectMultiByWhereClause(Criteria.create().andEqualTo("freight_id", template.getId()).build());

			FreightRules defaultRules = null;
			FreightRules rules = null;
			int maxFirstAmount = 0;
			for (FreightRules _rules : _rulesList) {
				// 默认运费
				if (_rules.getDefaul()) {
					defaultRules = _rules;
					continue;
				}
				// 配送地区匹配
				Map<String, List<String>> regions = _rules.getRegionInternal();
				// 不包含配送省份
				if (!regions.containsKey(address.getProvince())) {
					continue;
				}
				List<String> citys = regions.get(address.getProvince());
				// 不包含配送城市
				if (!CollectionUtils.isEmpty(citys) && !citys.contains(address.getCity())) {
					continue;
				}
				// 包含配送城市，并且为最大首费
				if (rules == null || _rules.getFirstAmount() > maxFirstAmount) {
					maxFirstAmount = _rules.getFirstAmount();
					rules = _rules;
				}
			}
			goods.setFreightRules(rules != null ? rules : defaultRules);
		}

		// 找出最大收费
		int maxFirstAmount = 0;
		for (OrderGoods goods : goodsList) {
			if (goods.getFreightTemplate().getInclPostage()) {
				continue;
			}
			FreightRules rules = goods.getFreightRules();
			if (rules.getFirstAmount() > maxFirstAmount) {
				maxFirstAmount = rules.getFirstAmount();
			}
		}

		// 首费+(增费金额*(实际购买/增费标准))
		BigDecimal total = new BigDecimal(maxFirstAmount);
		for (OrderGoods goods : goodsList) {
			if (goods.getFreightTemplate().getInclPostage()) {
				continue;
			}
			FreightRules rules = goods.getFreightRules();
			switch (goods.getFreightTemplate().getValuationModel()) {
				case 1:// 按件
					total = total.add(new BigDecimal(
							(rules.getSecondAmount() * (Math.ceil(Double.valueOf(goods.getBuyNumber()) / rules.getSecondPiece())))));
					break;
				case 2:// 按重量
					total = total.add(new BigDecimal(
							(rules.getSecondAmount() * (Math.ceil(goods.getWeight().doubleValue() / rules.getSecondWeight().doubleValue())))));
					break;
			}
		}
		return total;
	}
}
